package com.duoduo.homework.h1;

import java.awt.Point;

import javax.swing.Box;

import com.duoduo.algs4.util.Counter;
import com.duoduo.algs4.util.VisualAccumulator;

import edu.princeton.cs.algs4.Interval1D;
import edu.princeton.cs.algs4.Interval2D;
import edu.princeton.cs.algs4.Point2D;
import edu.princeton.cs.algs4.Stopwatch;
import edu.princeton.cs.introcs.StdDraw;
import edu.princeton.cs.introcs.StdOut;
import edu.princeton.cs.introcs.StdRandom;

public class IntervalRandom {

	public IntervalRandom() {
		// TODO Auto-generated constructor stub
	}

	public static void run(double xlo, double xhi, double ylo, double yhi, int trials) {
		Interval1D xinterval = new Interval1D(xlo, xhi);
		Interval1D yinterval = new Interval1D(ylo, yhi);
		Interval2D box = new Interval2D(xinterval, yinterval);
		box.draw();

		StdDraw.setPenColor(StdDraw.RED);
//		StdDraw.setPenRadius(.005);
		Counter counter = new Counter("计数器");
		for (int i = 0; i < trials; i++) {
			double x = StdRandom.random();
			double y = StdRandom.random();
			Point2D p = new Point2D(x, y);
			if(box.contains(p)){
				counter.increment();
			}else {
				p.draw();
			}
		}
		StdOut.println(counter);
		StdOut.println(box.area());

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Stopwatch watch = new Stopwatch(); 
		run(.2,.8,.5,.7,10000);
		StdOut.println(watch.elapsedTime());
		StdDraw.clear();
		watch = new Stopwatch();
		run(.2,.8,.5,.7,1000000);
		StdOut.println(watch.elapsedTime());

	}

}
